/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WaterLevel;

import javax.microedition.lcdui.*;
import javax.microedition.sensor.*;
import java.util.*;
import javax.microedition.io.Connector;
import javax.microedition.io.file.*;
import sUtils.*;


/**
 *
 * @author thomas.s
 */
public class WaterCanvas extends Canvas implements DataListener {

    private double mx=0, my=0, dx=0, dy=0, nx=0, ny=0, x0=0,y0=0;
    private double zx=0,zy=0, cx=0,cy=0,cz=0;
    private Timer timer;
    private String data=null;
    private double lastX[];
    private double lastY[];
    private double lastZ[];
    private int count=0;
    private int max=12;
    private Image bg=null;
    private Image blain=null;
    private double maxspeed=2;


    public WaterCanvas () {
        lastX = new double[max];
        lastY = new double[max];
        lastZ = new double[max];
       /* try {
            FileConnection f = (FileConnection) Connector.open("file:///filesystem/waterlevel_bg.jpg",Connector.READ);
            bg = Image.createImage(f.openInputStream());
        } catch (Exception e) { bg = null;}
        try { if (bg==null) bg = Image.createImage ( "/waterlevel_bg.jpg" );
        } catch (Exception e) { bg = null;}

        try {
            FileConnection f = (FileConnection) Connector.open("file:///filesystem/waterlevel_blain.png",Connector.READ);
            blain = Image.createImage(f.openInputStream());
        } catch (Exception e) { blain = null;}
        try { if (blain==null) blain = Image.createImage ( "/waterlevel_blain.png" );
        } catch (Exception e) { blain = null;}
        /*try {
                FileConnection f = (FileConnection) Connector.open("file:///filesystem/waterlevel_blain.jpg",Connector.READ);
                blain = Image.createImage(f.openInputStream());
                f.close();
            } catch (Exception e) { blain = null; }
        
*/
        bg = Images.load("file:///filesystem/waterlevel_bg.jpg","/waterlevel_bg.jpg");
        blain = Images.load("file:///filesystem/waterlevel_blain.png","/waterlevel_blain.png");
        timer = new Timer();
        timer.schedule(new updateTask(this), 0, 30 );
    }

    class updateTask extends TimerTask {
        private Canvas canvas;
        updateTask( Canvas c ){ canvas = c;  }
        public void run() {  canvas.repaint(); }
    }

    protected void paint (Graphics g) {
        int w = getWidth ();
        int h = getHeight ();
        int bw = 101;
        int bh = 102;
        double s = (double)w/8;

        // Fill the background
        if (bg==null) {
            g.setColor (0xa0a0c0);
            g.fillRect (0, 0, w, h);
            g.setColor (0xf0f0f0);
            g.setFont( Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ) );
            g.drawString( "stefanDev@handy-faq.de", 0,h-30, 0);

        } else
            g.drawImage(bg, 0, h-bg.getHeight(), 0);

        
        double x = w/2+mx*s;
        double y = h/2+my*s;
        if (x<0) x=0; else if (x>w) x=w;
        if (y<0) y=0; else if (y>h) y=h;

        if (blain==null)
        {
            g.setColor (0xa0a0f0);
            g.fillArc((int)(x-bw/2), (int)(y-bh/2), (int)bw, (int)bh, 0, 360);
        }
        else
            g.drawImage( blain,(int)(x-bw/2), (int)(y-bh/2), 0 );

        if (data!=null)
        {
            g.setColor (0xf0f0f0);
            g.setFont( Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ) );
            g.drawString( data, 1,1, 0);
        }

        g.setColor (0x404040);
        g.drawLine(w/2, 0, w/2, h);
        g.drawLine(0, h/2, w, h/2);

        dx = nx-mx; if (dx>maxspeed) dx=maxspeed; else if (dx<-maxspeed) dx=-maxspeed;
        dy = ny-my; if (dy>maxspeed) dy=maxspeed; else if (dy<-maxspeed) dy=-maxspeed;

        mx += dx*0.3;
        my += dy*0.3;

    }


    double correct( double v )
    {
        /*//v = Math.abs(v);
        double d=0;
        if (v<=60 && v>=-60) { d = (60-v)/60.0; v=45-d*45; } else
        { d = (v-60)/30.0; v=d*45+45; }
        */
        return v;
    }

    public void dataReceived( SensorConnection conn, Data[] arg, boolean lost )
    {
        double n=0;
        for (int i=max-2; i>=0; i--)
        {
            lastX[i+1]=lastX[i];
            lastY[i+1]=lastY[i];
            lastZ[i+1]=lastZ[i];
        }

        lastX[0] = arg[0].getDoubleValues()[0];
        lastY[0] = arg[1].getDoubleValues()[0];
        lastZ[0] = arg[2].getDoubleValues()[0];
        count++;
        if (count>max) count=max;
        cx=cy=cz=0;

        for (int j=0; j<count; j++)
        {
            cx += lastX[j]*(max-j);
            cy += lastY[j]*(max-j);
            cz += lastZ[j]*(max-j); n+=max-j;
        }
        cx /= n;
        cy /= n;
        cz /= n;

        cx = correct(cx);
        cy = correct(cy);
        cz = correct(cz);

        //StringBuffer s = new StringBuffer(30);
        //s.append(lastX[0]); s.append(", "); s.append(lastY[0]); s.append(", "); s.append(lastZ[0]);
        double f = 90/18;
        if (cz<0) cz=-cz;
        data = Strings.toStr(cx*f,3,1,' ',"","°, ") + Strings.toStr(-cy*f,3,1,' ',"","°, ") + Strings.toStr(cz*f,3,1,' ',"","°") ;
        nx = zx - cx + x0;
        ny = cy - zy + y0;

    }


    public void pointerPressed( int x, int y )
    {
        
    }

    public void setZero()
    {
        x0=cx; y0=-cy;
    }


}

